package cate.game.role.res.guild.po;

import cate.common.table.d.GDFunc;
import cate.common.table.d.GDGuild;
import cate.common.table.d.GDNotice;
import cate.common.util.GameResult;
import cate.game.chat.ChatContentParam;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import cate.game.role.Role;
import cate.game.role.res.guild.GuildFuncBase;
import cate.game.role.res.guild.msg.basic.GuildRecruitResp;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.ArrayList;
import java.util.List;

@NoteClass(value = "公会招募")
public class GuildRecruitmentPO extends GuildFuncBase {

    @NoteField(value = "当日发布招募次数")
    public int recruitmentNum;

    @NoteField(value = "上次招募时间")
    public long lastRecruitmentTime;


    public void onDaySpan() {
        recruitmentNum = 0;
    }

    /**
     * 发布公会招募信息
     */
    public void publish(Role role) {
        if(!guildFuncOpenCheck(role)){
            return;
        }
        byte pos = role.getRes().guild.getPos();
        if (pos == GDGuild.position.MEMBER) {
            role.getGame().notice.message(role, 903063, "只有公会管理者可以发布招募消息");
            return;
        }

        long inverter = (System.currentTimeMillis() - lastRecruitmentTime) / 1000;
        if (inverter < GDGuild.recruit.RECRUIT_INTERVAL_SECEND_MIN) {
            role.getGame().notice.message(role, 903064, "请{}秒后再次操作", (GDGuild.recruit.RECRUIT_INTERVAL_SECEND_MIN - inverter));
            return;
        }
        if (recruitmentNum >= GDGuild.recruit.DAILY_RECRUIT_NUM_MAX) {
            role.getGame().notice.message(role, 903065, "招募次数不足");
            return;
        }
        if (recruitmentNum >= GDGuild.recruit.DAILY_FREE_RECRUIT_TIME) {
            GameResult<MixResItem> r = new MixRes(GDGuild.recruit.RECRUIT_COST).consume(role, null);
            if (!r.ok()) {
                role.getGame().notice.message(role ,"消耗不足");
                return;
            }
        }
        // 发布招募消息
        sendSystem(role);
        //
        // 记录本次招募信息
        lastRecruitmentTime = System.currentTimeMillis();
        recruitmentNum++;
        //
        noticeUpdate(role);
        role.getGame().notice.message(role, 903066, "成功发布招募消息");
    }

    private void sendSystem(Role role) {
        List<ChatContentParam> params = new ArrayList<>();
        GuildPO guild = getFuncDependent().guild;
        params.add(ChatContentParam.c(guild.basic.level));
        params.add(ChatContentParam.c(guild.basic.guildName));
        params.add(ChatContentParam.c(guild.basic.levelRequired));
        // 公会id给前端用
        params.add(ChatContentParam.c(ChatContentParam.TYPE_GUILD_ID, guild.uid));
        //跑马灯
        role.getGame().notice.lampOnline(role, GDNotice.TextId.GUILD_RECRUIT, params);
    }

    public void noticeUpdate(Role role) {
        role.sendNow(new GuildRecruitResp(this));
    }

    @Override
    public int getFuncId() {
        return GDFunc.GuildFunc.MEMBER;
    }
}
